package com.zs.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

public class ZSDateUtils extends DateUtils {

    private static final String[] weekDays = {"（周日）", "（周一）", "（周二）", "（周三）", "（周四）", "（周五）", "（周六）"};

    private final static Logger logger = LoggerFactory.getLogger(ZSDateUtils.class);

    private ZSDateUtils() {
        throw new IllegalStateException("请勿实例化工具类");
    }

    private static ThreadLocal<Map<String, SimpleDateFormat>> local = new ThreadLocal<>();

    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_MONTH_FORMAT = "yyyy-MM";
    public static final String DATE_FORMAT_NO_DELIMITER = "yyyyMMdd";
    public static final String TIME_FORMAT = "HH:mm:ss";
    public static final String TIME_FORMAT_NO_SEC = "HH:mm";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATE_TIME_FORMAT_NO_SEC = "yyyy-MM-dd HH:mm";
    public static final String DATE_TIME_FORMAT_NO_DELIMITER = "yyyyMMddHHmmss";
    public static final String DATE_PRECISE_TO_MINUTE = "yyyyMMddHHmm";
    public static final String[] WEEKS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    public static final String[] DEFAULT_FORMATS = {TIMESTAMP_FORMAT, DATE_TIME_FORMAT, DATE_FORMAT, TIME_FORMAT, DATE_PRECISE_TO_MINUTE};

    public static final String DAY_BEGIN_TIME = " 00:00:00";
    public static final String DAY_END_TIME = " 23:59:59";

    private static SimpleDateFormat getDateFormat(String format) {
        Map<String, SimpleDateFormat> map = local.get();
        if (map == null) {
            map = new HashMap<>();
            local.set(map);
        }
        SimpleDateFormat sdf = map.get(format);
        if (sdf == null) {
            sdf = new SimpleDateFormat(format);
            map.put(format, sdf);
        }
        return sdf;
    }
    private static void remove(){local.remove();}


    private static SimpleDateFormat getDateFormatDefault() {
        return getDateFormat(DATE_TIME_FORMAT);
    }

    private static SimpleDateFormat getDateFormatDate() {
        return getDateFormat(DATE_FORMAT);
    }

    private static SimpleDateFormat getDateFormatTime() {
        return getDateFormat(TIME_FORMAT);
    }

    /**
     * 自动判断日期字符串的格式，返回Date对象
     *
     * @param dateString 日期字符串
     * @param dateFormat 格式字符串数组。为空时使用<code>DateUtil.DEFAULT_FORMATS</code>
     * @return 日期Date对象
     * @throws ParseException
     * @see DateUtils#parseDate
     */
    public static Date parse(String dateString, String... dateFormat) throws ParseException {
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }

        if (dateFormat == null || dateFormat.length == 0) {
            return DateUtils.parseDate(dateString, DEFAULT_FORMATS);
        } else {
            return DateUtils.parseDate(dateString, dateFormat);
        }
    }

    /**
     * 取指定格式的当前时间字符串
     *
     * @param dateFormat
     * @return
     */
    public static String getCurrentTime(String dateFormat) {
        Date date = new Date();
        return format(date, dateFormat);
    }

    /**
     * 将字符串转换成Date类型
     *
     * @param dateString
     * @param dateFormat
     * @return
     */
    public static Date parse(String dateString, String dateFormat) {
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        try {
            return DateUtils.parseDate(dateString, dateFormat);
        } catch (ParseException e) {
            logger.error("日期格式化异常", e);
            return null;
        }
    }

    /**
     * 将Date类型转化成字符串
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static String format(Date date, String dateFormat) {
        if (date == null) {
            return "";
        } else {
            return DateFormatUtils.format(date, dateFormat);
        }
    }

    /**
     * 在传入的日期基础上往后加n天
     *
     * @param date
     * @param n    要加的天数
     * @return
     */
    public static Date addDay(Date date, int n) {
        return DateUtils.addDays(date, n);
    }


    /**
     * 在传入的日期基础上往后加n天
     *
     * @param dateStr
     * @param n       要加的天数
     * @return
     */
    public static String addDay(String dateStr, String dateFormat, int n) {
        if (StringUtils.isBlank(dateStr) || StringUtils.isBlank(dateFormat)) {
            return "";
        }
        Date date = parse(dateStr, dateFormat);
        Date newDate = addDay(date, n);
        return format(newDate, dateFormat);
    }

    /**
     * 判断当前时间是否在开始时间与结束时间之间
     *
     * @param time  当前时间
     * @param begin 开始时间
     * @param end   结束时间
     * @return boolen类型，true表示在两者间，false表示不在两者之间
     */
    public static boolean isTimeIn(Date time, Date begin, Date end) {
        return time.getTime() >= begin.getTime() && time.getTime() <= end.getTime();
    }

    /**
     * 判断当前时间是否在开始时间与结束时间之间
     *
     * @param time  当前时间
     * @param begin 开始时间
     * @param end   结束时间
     * @return boolen类型，true表示在两者间，false表示不在两者之间(time和end相同时，为false)
     */
    public static boolean isTimeIn1(Date time, Date begin, Date end) {
        return time.getTime() >= begin.getTime() && time.getTime() < end.getTime();
    }

    /**
     * 判断指定日期是星期几
     *
     * @param time   要判断的日期
     * @param format 输入的日期格式
     * @return 返回数字[1:星期一，2：星期二，....，7：星期日]
     * @throws ParseException
     */
    public static int getWeek(String time, String format) throws ParseException {
        return getWeek(DateUtils.parseDate(time, format));
    }

    /**
     * 判断指定日期是星期几
     *
     * @param date 要判断的日期
     * @return 返回数字[1:星期一，2：星期二，....，7：星期日]
     * @throws ParseException
     */
    public static int getWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int week = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            week = 7;
        } else {
            week = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return week;
    }

    /**
     * 获取一年的第几周，默认周一是第一天（按中国习惯）
     *
     * @param date            -
     * @param isInternational - 是否国际化，true：周日为第一天，false：周一为第一天，即周日会比国际通用的weekofyear值少1
     * @return -
     */
    public static int getWeekOfYear(Date date, Boolean isInternational) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int i = c.get(Calendar.WEEK_OF_YEAR);
        int week = getWeek(date);
        if (isInternational != null && !isInternational && week == 7) {
            i = i - 1;
        }
        return i;
    }

    /**
     * 获取一年的第几周
     *
     * @param time            -
     * @param format          -
     * @param isInternational - 是否国际化，true：周日为第一天，false：周一为第一天
     * @return -
     */
    public static int getWeekOfYear(String time, String format, Boolean isInternational) {
        return getWeekOfYear(parse(time, format), isInternational);
    }


    /**
     * 判断是否为有效的身份证日期
     *
     * @param date
     * @return
     */
    public static boolean isIdDate(String date) {
        return isDateFormat(date, "yyyyMMdd");
    }

    /**
     * 判断传入的字符串dateStr是否是日期格式patternStr的字符串 @author yejg
     *
     * @param dateStr
     * @param patternStr
     * @return
     */
    public static boolean isDateFormat(String dateStr, String patternStr) {
        Date date = null;
        try {
            date = parse(dateStr, patternStr);
        } catch (Exception e) {
        }

        return date == null ? false : true;
    }

    /**
     * 将字符串日期转成Timestamp类型
     *
     * @param dateString 字符串类型的时间
     * @param format     字符串类型的时间要转换的格式
     * @return Timestamp类型的时间戳
     * @throws ParseException
     */
    public static java.sql.Timestamp parse2Timestamp(String dateString, String format) throws ParseException {
        return new java.sql.Timestamp(DateUtils.parseDate(dateString, format).getTime());
    }

    /**
     * 获取两个时间的间隔,字符串表示
     *
     * @param start
     * @param end
     * @return
     * @author huadi
     */
    public static String getDiffTimeStr(Date start, Date end) {
        String time = "";
        if (start != null && end != null) {
            int t = (int) (end.getTime() - start.getTime()) / 1000;
            String h = "";
            String m = "";
            String s = "";
            h = (int) t / 3600 + "";
            m = (int) (t % 3600) / 60 + "";
            s = t % 60 + "";
            if (h.length() <= 1) {
                h = "0" + h;
            }
            if (m.length() <= 1) {
                m = "0" + m;
            }
            if (s.length() <= 1) {
                s = "0" + s;
            }
            time = h + ":" + m + ":" + s;
        }
        return time;
    }

    /**
     * 获取两个日期之间间隔的分钟数
     *
     * @param startDate
     * @param endDate
     * @return
     * @author zhougz
     */
    public static int getIntervalMinute(Date startDate, Date endDate) {
        int min = 0;
        if (null != startDate && null != endDate) {
            long end = endDate.getTime();
            long start = startDate.getTime();
            long betweenDate = (end - start) / (60 * 1000);
            min = Long.valueOf(betweenDate).intValue();
        }
        return min;
    }

    /**
     * 获取两个日期之间间隔的天数
     *
     * @param start_date
     * @param end_date
     * @return
     * @author sunyy
     */
    public static int getIntervalDay(Date start_date, Date end_date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            start_date = sdf.parse(sdf.format(start_date));
            end_date = sdf.parse(sdf.format(end_date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(start_date);
        long time1 = cal.getTimeInMillis();
        cal.setTime(end_date);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }


    /**
     * 星期转换为星期索引
     *
     * @param week
     * @return
     */
    public static int weekToNum(String week) {
        int weekNum = -1;
        for (int i = 0, j = WEEKS.length; i < j; i++) {
            if (week != null && WEEKS[i].toLowerCase().contains(week.toLowerCase())) {
                weekNum = i + 1;
                break;
            }
        }
        return weekNum;
    }

    /**
     * 获取日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);//时
        cal.set(Calendar.MINUTE, 0);//分
        cal.set(Calendar.SECOND, 0);//秒
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        Date resultDate = cal.getTime();
        return resultDate;
    }

    /**
     * 获取日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);//时
        cal.set(Calendar.MINUTE, 59);//分
        cal.set(Calendar.SECOND, 59);//秒
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, 6);
        cal.add(Calendar.DAY_OF_WEEK, d);
        Date resultDate = cal.getTime();
        return resultDate;
    }

    /**
     * 获取日期当月第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);//时
        c.set(Calendar.MINUTE, 0);//分
        c.set(Calendar.SECOND, 0);//秒
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        Date resultDate = c.getTime();
        return resultDate;
    }

    /**
     * 获取日期当月最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);//时
        c.set(Calendar.MINUTE, 59);//分
        c.set(Calendar.SECOND, 59);//秒
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date resultDate = c.getTime();
        return resultDate;
    }

    /**
     * 获取今天剩余秒数
     *
     * @return
     */
    public static int getTodaySeconds() {
        Calendar curDate = Calendar.getInstance();
        Calendar tommorowDate = new GregorianCalendar(curDate
                .get(Calendar.YEAR), curDate.get(Calendar.MONTH), curDate
                .get(Calendar.DATE) + 1, 0, 0, 0);
        return (int) (tommorowDate.getTimeInMillis() - curDate.getTimeInMillis()) / 1000;
    }

    /**
     * 获取当前时间字符串（yyyy-MM-dd HH:mm:ss）
     *
     * @return 当前时间字符串
     */
    public static String getNowStr() {
        return getDatetimeStr(new Date());
    }

    /**
     * Date转String
     *
     * @param date 时间
     * @return 字符串（yyyy-MM-dd HH:mm:ss）
     */
    public static String getDatetimeStr(Date date) {
        return getDateFormatDefault().format(date);
    }

    /**
     * 获取当前时间字符串（yyyy-MM-dd）
     *
     * @return 当前时间字符串
     */
    public static String getNowDateStr() {
        return getDateFormatDate().format(new Date());
    }

    /**
     * 获取当前时间字符串（HH:mm:ss）
     *
     * @return 当前时间字符串
     */
    public static String getNowTimeStr() {
        return getDateFormatTime().format(new Date());
    }

    /**
     * 计算今天到指定时间的剩余天数
     * <p>1.不足一天按一天算；</p>
     * <p>2.指定时间是过去时间，返回0；</p>
     *
     * @param date 指定时间
     * @return 剩余天数
     */
    public static Integer decorateDays(Date date) {
        Long d = date.getTime() - new Date().getTime();
        if (d < 0L) {
            return 0;
        }
        d = d / 3600_000L / 24L;
        return d.intValue() + 1;
    }

    /**
     * Date转String
     *
     * @param date   时间
     * @param format 格式
     * @return 时间字符串
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat dateFormat = getDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return dateFormat.format(date);
    }

    /**
     * Date转String
     *
     * @param date 时间
     * @return 时间字符串（yyyy-MM-dd HH:mm:ss）
     */
    public static String formatDate(Date date) {
        return formatDate(date, DATE_TIME_FORMAT);
    }

    /**
     * String转Date
     *
     * @param dateStr 时间字符串
     * @param format  格式
     * @return Date 时间
     * @throws ParseException
     */
    public static Date parseDate(String dateStr, String format) throws ParseException {
        return getDateFormat(format).parse(dateStr);
    }

    /**
     * String转Date
     *
     * @param dateStr 时间字符串（yyyy-MM-dd HH:mm:ss）
     * @return Date 时间
     * @throws ParseException
     */
    public static Date parseDate(String dateStr) throws ParseException {
        return parseDate(dateStr, DATE_TIME_FORMAT);
    }

    /**
     * 计算时间差（毫秒）
     *
     * @param d1 第一个时间
     * @param d2 第二个时间
     * @return d1.getTime() - d2.getTime()
     */
    public static long calcTimeDifference(Date d1, Date d2) {
        return d1.getTime() - d2.getTime();
    }

    /**
     * 计算传入时间和当前时间相比已经过去了多少分钟
     *
     * @param date 传入时间
     * @return 传入时间和当前时间相比已经过去了多少分钟
     */
    public static long calcPastMinute(Date date) {
        long diffMillisecond = calcTimeDifference(new Date(), date);
        return diffMillisecond / 60_000L;
    }

    /**
     * 计算传入时间和当前时间相比已经过去了多少分钟
     *
     * @param dateStr 传入时间字符串（yyyy-MM-dd HH:mm:ss）
     * @return 传入时间和当前时间相比已经过去了多少分钟
     */
    public static long calcPastMinute(String dateStr) throws ParseException {
        long diffMillisecond = calcTimeDifference(new Date(), parseDate(dateStr));
        return diffMillisecond / 60_000L;
    }

    /**
     * 获取N分钟之前的时间
     *
     * @param minute 单位分钟
     * @return N分钟之前的时间
     */
    public static Date getMinuteAgoDate(long minute) {
        long agoTimestamp = System.currentTimeMillis() - minute * 60_000L;
        return new Date(agoTimestamp);
    }

    /**
     * 获取之前的时间
     *
     * @param day    N天
     * @param hour   N小时
     * @param minute N分钟
     * @param second N秒
     * @return 之前的时间
     */
    public static Date getAgoDate(long day, long hour, long minute, long second) {
        long agoTimestamp = System.currentTimeMillis() - (((day * 24 + hour) * 60 + minute) * 60 + second) * 1000L;
        return new Date(agoTimestamp);
    }

    /**
     * 获取基准时间之前的时间
     *
     * @param baseDate 基准时间
     * @param day      N天
     * @param hour     N小时
     * @param minute   N分钟
     * @param second   N秒
     * @return 之后的时间
     */
    public static Date getAfterDate(Date baseDate, long day, long hour, long minute, long second) {
        long currTimestamp = baseDate.getTime();
        long afterTimestamp = currTimestamp + (((day * 24 + hour) * 60 + minute) * 60 + second) * 1000L;
        return new Date(afterTimestamp);
    }

    /**
     * 获取指定月份第一天Date
     *
     * @param dateStr yyyy-MM
     * @return Date类型时间
     */
    public static Date getFirstDayByMonth(String dateStr) throws Exception {
        dateStr = dateStr + "-01";
        return parseDate(dateStr, "yyyy-MM-dd");
    }

    /**
     * 获取指定月份下个月第一天Date
     *
     * @param dateStr
     * @return
     */
    public static Date getNextFirstDayByMonth(String dateStr) throws Exception {
        Date date = getFirstDayByMonth(dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 获取指定月份最后一天Date
     *
     * @param dateStr yyyy-MM
     * @return Date类型时间
     */
    public static Date getLastDayByMonth(String dateStr) throws Exception {
        Date date = getFirstDayByMonth(dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    /**
     * <p>功能描述: 获取当前时间距离一天结束的剩余秒数</p>
     *
     * @param currentDate -
     * @return <p>修改履历：</p>
     * @date 2019/9/5 19:28
     */
    public static Integer getRemainSecondsOneDay(Date currentDate) {
        Calendar midnight = Calendar.getInstance();
        midnight.setTime(currentDate);
        midnight.add(Calendar.DAY_OF_MONTH, 1);
        midnight.set(Calendar.HOUR_OF_DAY, 0);
        midnight.set(Calendar.MINUTE, 0);
        midnight.set(Calendar.SECOND, 0);
        midnight.set(Calendar.MILLISECOND, 0);
        return Long.valueOf((midnight.getTime().getTime() - currentDate.getTime()) / 1000).intValue();
    }

    /**
     * <p>功能描述: 转换两个时间的时间差为天时分秒</p>
     *
     * @param beginDate -计算开始时间
     * @param endDate   -计算结束时间
     * @return <p>修改履历：</p>
     * @author jishu
     * @date 2019/9/24 10:18
     */
    public static Map<String, Integer> parseDayHourMinuteSecond(Date beginDate, Date endDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - beginDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("day", Long.valueOf(day).intValue());
        resultMap.put("hour", Long.valueOf(hour).intValue());
        resultMap.put("minute", Long.valueOf(min).intValue());
        resultMap.put("second", Long.valueOf(sec).intValue());
        return resultMap;
    }

    /**
     * 通过日期获取周几，格式如：7-08（周三）
     *
     * @param date -
     * @return -
     */
    public static String dateToDayOfWeekStr(Date date) {
        Calendar calendar = Calendar.getInstance();
        StringBuilder sb = new StringBuilder();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        if (month < 10) {
            sb.append("0");
        }
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        sb.append(month).append("-").append(day);

        int i = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (i < 0) {
            i = 0;
        }
        sb.append(weekDays[i]);
        return sb.toString();
    }

    /**
     * 通过日期获取小时，格式如：01:00
     *
     * @param date -
     * @return -
     */
    public static String dateToHourStr(Date date) {
        return ZSDateUtils.format(date, "HH:mm");
    }

    /**
     * 通过日期获取一年中的第几周
     *
     * @param date -
     * @return -
     */
    public static String dateToWeekOfYearStr(Date date) {
        Calendar calendar = Calendar.getInstance();
        StringBuilder sb = new StringBuilder();
        calendar.setTime(date);
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        sb.append("第").append(weekOfYear).append("周");
        return sb.toString();
    }

    /**
     * 通过日期获取几月
     *
     * @param date -
     * @return -
     */
    public static String dateToMonthOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        StringBuilder sb = new StringBuilder();
        calendar.setTime(date);
        int month1 = calendar.get(Calendar.MONTH) + 1;
        sb.append("第").append(month1).append("月");
        return sb.toString();
    }

    /**
     * 转yyyy-MM-dd 为yyyy-MM-dd HH:mm:ss
     *
     * @param date -
     * @param type - 1.每天的开始 2.每天的结束
     * @return -
     */
    public static String transDateToDateTime(String date, Integer type) {
        if (StringUtils.length(date) == 10) {
            if (type == 1) {
                return date + DAY_BEGIN_TIME;
            } else {
                return date + DAY_END_TIME;
            }
        }else{
            return date;
        }
    }

    public static Date getFirstDateOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_YEAR, c.getActualMinimum(Calendar.DAY_OF_YEAR));
        return setDayMinTime(c.getTime());
    }
    public static Date setDayMinTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, c.getActualMinimum(Calendar.HOUR_OF_DAY));
        c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
        c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
        c.set(Calendar.MILLISECOND, c.getActualMinimum(Calendar.MILLISECOND));
        return c.getTime();
    }
    public static List<Date> getDaysBetweenDates(Date startDate,Date endDate) {
        List<Date> dates = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        while (c.getTime().before(endDate)) {
            dates.add(c.getTime());
            c.add(Calendar.DAY_OF_MONTH, 1);
        }
        dates.add(endDate);
        return dates;
    }
    public static List<String> getDaysStrBetweenDates(Date startDate,Date endDate) {
        List<String> dates = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        while (c.getTime().before(endDate)) {
            dates.add(formatDate(c.getTime(),DATE_FORMAT));
            c.add(Calendar.DAY_OF_MONTH, 1);
        }
        dates.add(formatDate(endDate,DATE_FORMAT));
        return dates;
    }
}
